#include "historywindow.h"
#include <QMainWindow>
#include <QWidget>
#include <QTableWidget>
#include <QHeaderView>
#include <QFile>
#include <QTextStream>
#include <QString>
#include <QStringList>
#include <QTableWidgetItem>
#include <QHeaderView>
#include <QFile>
#include <QTextStream>

HistoryWindow::HistoryWindow(QWidget *parent) : QMainWindow(parent)
{
    setWindowTitle("BMI历史记录");
    resize(600, 400);
    
    table = new QTableWidget(this);
    table->setColumnCount(3);
    table->setHorizontalHeaderLabels({"日期", "BMI值", "健康状态"});
    table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    table->setStyleSheet("QTableWidget { font-size: 12pt; }");
    
    setCentralWidget(table);
}

void HistoryWindow::loadHistoryData(const QString &filePath)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    table->setRowCount(0);
    QTextStream stream(&file);
    while (!stream.atEnd()) {
        QString line = stream.readLine();
        QStringList parts = line.split(",");
        if (parts.size() == 3) {
            int row = table->rowCount();
            table->insertRow(row);
            table->setItem(row, 0, new QTableWidgetItem(parts[0]));
            table->setItem(row, 1, new QTableWidgetItem(parts[1]));
            table->setItem(row, 2, new QTableWidgetItem(parts[2]));
        }
    }
    file.close();
}